#!/bin/bash
set -ex

main(){
  local backend="$1"
  if [ ! -x "$backend" ]; then
    printf "Unknow executable file: $backend\n" >&2
    return 1
  fi
  
  local src="$(mktemp)"
  echo hello > "$src"
  
  local name1="$(uuidgen)"
  local name2="$(uuidgen)"
  echo hello | "$backend" push "$src" "$name1"
  echo hello | "$backend" push "$src" "$name2/$name1"
  test "$(curl -sL "$("$backend" pull "$name2/$name1")")" == hello
  "$backend" rm "$name2"
  rm -rf "$src"
  
  "$backend" ls | grep -q "^/$name1\$"
  "$backend" ls / | grep -q "^/$name1\$"
  test "$(curl -sL "$("$backend" pull "$name1")")" == hello
  
  "$backend" mv "$name1" "$name2/$name1"
  "$backend" ls | grep -q "^/$name2/\$"
  "$backend" ls "$name2" | grep -q "^/$name2/$name1\$"
  test "$(curl -sL "$("$backend" pull "$name2/$name1")")" == hello
  
  "$backend" rm "$name2/$name1"
  if "$backend" ls "$name2" | grep -q "^/$name2/$name1\$"; then
    return 1
  fi
  "$backend" rm "$name2"
  if "$backend" ls | grep -q "^/$name2/\$"; then
    return 1
  fi
}

main "$@"
